﻿using Ledict.ConditionExtract.Model;
using Ledict.Core.Data;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Ledict.ConditionExtract.DAL
{
    public class RuleInfosDAL
    {
        private string DbName;
        public RuleInfosDAL()
        {
            DbName = "LdtCommonManage";
        }

        /// <summary>
        /// 删除RuleInfoParmeterRelation表数据
        /// </summary>
        public bool RuleInfoParmeterRelationDelete(string rIuId, string plId)
        {
            string sql = string.Format(@" DELETE FROM [dbo].[RuleInfoParmeterRelation] WHERE RIU_ID = '{0}' AND PL_ID = '{1}' ", rIuId, plId);
            Database oBase = DatabaseFactory.CreateDatabase(DbName);
            DbCommand oCmd = oBase.GetSqlStringCommand(sql);
            if (oBase.ExecuteNonQuery(oCmd) > 0)
            {
                return true;
            }
            return false;
        }

        /// <summary>
        /// 删除Riu_ParameterList表数据
        /// </summary>
        public bool Riu_ParameterDelete(string rIuId, string plId)
        {
            string sql = string.Format(@" DELETE FROM [dbo].[Riu_ParameterList] WHERE PL_ID = '{0}'; DELETE FROM [dbo].[RuleInfoParmeterRelation]  WHERE RIU_ID = '{1}' AND PL_ID = '{0}'", plId, rIuId);
            Database oBase = DatabaseFactory.CreateDatabase(DbName);
            DbCommand oCmd = oBase.GetSqlStringCommand(sql);
            if (oBase.ExecuteNonQuery(oCmd) > 0)
            {
                return true;
            }
            return false;
        }

        /// <summary>
        /// 查询PLID是否有多条
        /// </summary>
        public string GetPLIDCount(string rIuId,string plId)
        {
            string sql = string.Format(@" SELECT COUNT(1) AS num FROM [dbo].[RuleInfoParmeterRelation]  WHERE PL_ID = '{0}' AND RIU_ID <> '{1}' ",plId,rIuId);
            Database oBase = DatabaseFactory.CreateDatabase(DbName);
            DbCommand oCmd = oBase.GetSqlStringCommand(sql);
            var obj = oBase.ExecuteScalar(oCmd).ToString();
            if (!string.IsNullOrEmpty(obj) )
            {
                return obj.ToString();
            }
            return null;
        }

        /// <summary>
        /// 获取RuleInfoParmeterRelation表参数数据
        /// </summary>
        public DataTable RuleInfoParmeterRelationData(string rIuId)
        {
            string strSql = string.Format(@" SELECT PL_ID FROM [dbo].[RuleInfoParmeterRelation] WHERE RIU_ID='{0}'  ", rIuId);
            Database oBase = DatabaseFactory.CreateDatabase(DbName);
            DbCommand oCmd = oBase.GetSqlStringCommand(strSql.ToString());
            DataTable dt = oBase.ExecuteDataSet(oCmd).Tables[0];
            if (dt != null && dt.Rows.Count > 0)
            {
                return dt;
            }
            return new DataTable();
        }

        /// <summary>
        /// 获取Riu_RuleRelation表参数数据
        /// </summary>
        public DataTable Riu_RuleRelationData(string rIuId)
        {
            string strSql = string.Format(@" SELECT RR_RelationTable_ID FROM [dbo].[Riu_RuleRelation] WHERE RIU_ID = '{0}'  ", rIuId);
            Database oBase = DatabaseFactory.CreateDatabase(DbName);
            DbCommand oCmd = oBase.GetSqlStringCommand(strSql.ToString());
            DataTable dt = oBase.ExecuteDataSet(oCmd).Tables[0];
            if (dt != null && dt.Rows.Count > 0)
            {
                return dt;
            }
            return new DataTable();
        }

        /// <summary>
        /// 查询PLID是否有多条
        /// </summary>
        public DataTable RR_RelationTable_IDNum(string rIuId, string rrrId)
        {
            string sql = string.Format(@" SELECT RR_ExecuteMode  FROM [dbo].[Riu_RuleRelation] WHERE RIU_ID <> '{0}' AND RR_RelationTable_ID = '{1}' ", rIuId, rrrId);
            Database oBase = DatabaseFactory.CreateDatabase(DbName);
            DbCommand oCmd = oBase.GetSqlStringCommand(sql);
            DataTable dt = oBase.ExecuteDataSet(oCmd).Tables[0];
            if (dt != null && dt.Rows.Count > 0)
            {
                return dt;
            }
            return new DataTable();
        }

        /// <summary>
        /// 删除RuleInfoParmeterRelation表数据
        /// </summary>
        public bool Riu_RuleRelationDelete(string rIuId, string rrId)
        {
            string sql = string.Format(@"DELETE FROM [dbo].[Riu_RuleRelation]  WHERE RIU_ID = '{0}' AND RR_RelationTable_ID = '{1}' ", rIuId, rrId);
            Database oBase = DatabaseFactory.CreateDatabase(DbName);
            DbCommand oCmd = oBase.GetSqlStringCommand(sql);
            if (oBase.ExecuteNonQuery(oCmd) > 0)
            {
                return true;
            }
            return false;
        }

        /// <summary>
        /// 删除RuleInfoParmeterRelation表数据
        /// </summary>
        public bool Riu_ParameterDelete(string rrId, int rrExecuteMode)
        {
            string sql = string.Empty;
            switch (rrExecuteMode)
            {
                case 0:
                    sql = string.Format(@" DELETE FROM [dbo].[Riu_HistoryInfo] WHERE HI_OutID = '{0}';DELETE FROM [dbo].[Riu_Regexs] WHERE ER_ID = '{0}', rrId");
                    break;
                case 1:
                    sql = string.Format(@" DELETE FROM [dbo].[Riu_HistoryInfo] WHERE HI_OutID  = '{0}';DELETE FROM[dbo].[Riu_CodeRule] WHERE CR_ID= '{0}', rrId");
                    break;
                case 2:
                    sql = string.Format(@" DELETE FROM [dbo].[Riu_HistoryInfo] WHERE HI_OutID =  '{0}';DELETE FROM [dbo].[Riu_AIRule] WHERE AR_ID = '{0}', rrId");
                    break;
            }
            if (!string.IsNullOrEmpty(sql))
            {
                Database oBase = DatabaseFactory.CreateDatabase(DbName);
                DbCommand oCmd = oBase.GetSqlStringCommand(sql);
                if (oBase.ExecuteNonQuery(oCmd) > 0)
                {
                    return true;
                }
            }
            return false;
        }

        /// <summary>
        /// 删除RuleInfoUnit表数据
        /// </summary>
        public bool RuleInfoUnitDelete(string rIuId)
        {
            string sql = string.Format(@" DELETE FROM [dbo].[RuleInfoUnit] WHERE RIU_ID = '{0}'", rIuId);
            Database oBase = DatabaseFactory.CreateDatabase(DbName);
            DbCommand oCmd = oBase.GetSqlStringCommand(sql);
            if (oBase.ExecuteNonQuery(oCmd) > 0)
            {
                return true;
            }
            return false;
        }
    }
}
